package com.yiren.backstage.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.yiren.backstage.currency.entity.StateMonitorEntity;

import com.yiren.backstage.currency.entity.MonitorCountEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "monitor_count".
*/
public class MonitorCountEntityDao extends AbstractDao<MonitorCountEntity, String> {

    public static final String TABLENAME = "monitor_count";

    /**
     * Properties of entity MonitorCountEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property State_monitor_id = new Property(1, String.class, "state_monitor_id", false, "STATE_MONITOR_ID");
        public final static Property LogicConnective = new Property(2, String.class, "logicConnective", false, "LOGIC_CONNECTIVE");
        public final static Property RelationalOperator = new Property(3, String.class, "relationalOperator", false, "RELATIONAL_OPERATOR");
        public final static Property CarStateName = new Property(4, String.class, "carStateName", false, "CAR_STATE_NAME");
        public final static Property Value = new Property(5, String.class, "value", false, "VALUE");
        public final static Property MinValue = new Property(6, String.class, "minValue", false, "MIN_VALUE");
        public final static Property MaxValue = new Property(7, String.class, "maxValue", false, "MAX_VALUE");
    }

    private DaoSession daoSession;


    public MonitorCountEntityDao(DaoConfig config) {
        super(config);
    }
    
    public MonitorCountEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, MonitorCountEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String state_monitor_id = entity.getState_monitor_id();
        if (state_monitor_id != null) {
            stmt.bindString(2, state_monitor_id);
        }
 
        String logicConnective = entity.getLogicConnective();
        if (logicConnective != null) {
            stmt.bindString(3, logicConnective);
        }
 
        String relationalOperator = entity.getRelationalOperator();
        if (relationalOperator != null) {
            stmt.bindString(4, relationalOperator);
        }
 
        String carStateName = entity.getCarStateName();
        if (carStateName != null) {
            stmt.bindString(5, carStateName);
        }
 
        String value = entity.getValue();
        if (value != null) {
            stmt.bindString(6, value);
        }
 
        String minValue = entity.getMinValue();
        if (minValue != null) {
            stmt.bindString(7, minValue);
        }
 
        String maxValue = entity.getMaxValue();
        if (maxValue != null) {
            stmt.bindString(8, maxValue);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, MonitorCountEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String state_monitor_id = entity.getState_monitor_id();
        if (state_monitor_id != null) {
            stmt.bindString(2, state_monitor_id);
        }
 
        String logicConnective = entity.getLogicConnective();
        if (logicConnective != null) {
            stmt.bindString(3, logicConnective);
        }
 
        String relationalOperator = entity.getRelationalOperator();
        if (relationalOperator != null) {
            stmt.bindString(4, relationalOperator);
        }
 
        String carStateName = entity.getCarStateName();
        if (carStateName != null) {
            stmt.bindString(5, carStateName);
        }
 
        String value = entity.getValue();
        if (value != null) {
            stmt.bindString(6, value);
        }
 
        String minValue = entity.getMinValue();
        if (minValue != null) {
            stmt.bindString(7, minValue);
        }
 
        String maxValue = entity.getMaxValue();
        if (maxValue != null) {
            stmt.bindString(8, maxValue);
        }
    }

    @Override
    protected final void attachEntity(MonitorCountEntity entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public MonitorCountEntity readEntity(Cursor cursor, int offset) {
        MonitorCountEntity entity = new MonitorCountEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // state_monitor_id
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // logicConnective
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // relationalOperator
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // carStateName
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // value
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // minValue
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7) // maxValue
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, MonitorCountEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setState_monitor_id(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setLogicConnective(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setRelationalOperator(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setCarStateName(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setValue(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setMinValue(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setMaxValue(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
     }
    
    @Override
    protected final String updateKeyAfterInsert(MonitorCountEntity entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(MonitorCountEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(MonitorCountEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getStateMonitorEntityDao().getAllColumns());
            builder.append(" FROM monitor_count T");
            builder.append(" LEFT JOIN state_monitor T0 ON T.\"STATE_MONITOR_ID\"=T0.\"ID\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected MonitorCountEntity loadCurrentDeep(Cursor cursor, boolean lock) {
        MonitorCountEntity entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        StateMonitorEntity stateMonitorEntity = loadCurrentOther(daoSession.getStateMonitorEntityDao(), cursor, offset);
        entity.setStateMonitorEntity(stateMonitorEntity);

        return entity;    
    }

    public MonitorCountEntity loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<MonitorCountEntity> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<MonitorCountEntity> list = new ArrayList<MonitorCountEntity>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<MonitorCountEntity> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<MonitorCountEntity> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
